#include "../Include/BitmapRot.h"
#include <cmath>

BitmapRot::BitmapRot( HINSTANCE hAppInst, const CHAR *pImageFileName, const CHAR *pImageMaskFileName /*= NULL*/, const PositionType posType /*= POS_UPPER_LEFT*/, const BlendOptions bOp /*= BLEND_SRC_COPY*/ )
	: Bitmap( hAppInst, pImageFileName, posType, bOp ), m_hBMMask( NULL ), m_rotRad( 0.0f ), m_xRot( 0 ), m_yRot( 0 )/*, m_createPoly( false )*/
{
	if ( !pImageMaskFileName )
		return;

	// Mask needs to be loaded as monochrome
	m_hBMMask = (HBITMAP)LoadImage( hAppInst, pImageMaskFileName, IMAGE_BITMAP, 0, 0, LR_MONOCHROME | LR_LOADFROMFILE ); 
	try { PSX_ThrowAssert( m_hBMMask, "Failed to create image bitmap." ); }
	catch ( Exception e )
	{
		MessageBox( NULL, e.GetMessage(), "Error", MB_ICONERROR );
		return;
	}
	GetObject( m_hBMMask, sizeof( BITMAP ), &m_bMMInfo );
}

void BitmapRot::Render( HDC hBBDC, HDC hSprDC, INT x, INT y )
{
	TranslatePosition( x, y );

	// Do fun math stuff!!!
	INT w		= GetWidth();
	INT h		= GetHeight();
	INT left	= -m_xRot;
	INT top		= -m_yRot;
	INT right	=  left + w;
	INT bottom	=  top + h;

	FLOAT cosAngle		= cosf(m_rotRad);
	FLOAT sinAngle		= sinf(m_rotRad);
	FLOAT xRotLeftCos	= m_xRot + left*cosAngle;
	FLOAT yRotLeftSin	= m_yRot + left*sinAngle;
	FLOAT topSin		= top * sinAngle;
	FLOAT topCos		= top * cosAngle;
	FLOAT leftTopX		= (FLOAT)(xRotLeftCos - topSin);
	FLOAT leftTopY		= (FLOAT)(yRotLeftSin + topCos);
	FLOAT rightTopX		= (FLOAT)(m_xRot + right*cosAngle - topSin);
	FLOAT rightTopY		= (FLOAT)(m_yRot + right*sinAngle + topCos);
	FLOAT leftBottomX	= (FLOAT)(xRotLeftCos - bottom*sinAngle);
	FLOAT leftBottomY	= (FLOAT)(yRotLeftSin + bottom*cosAngle);

	m_polygon[0].x = (LONG)leftTopX + x;
	m_polygon[0].y = (LONG)leftTopY + y;
	m_polygon[1].x = (LONG)rightTopX + x;
	m_polygon[1].y = (LONG)rightTopY + y;
	m_polygon[2].x = (LONG)leftBottomX + x;
	m_polygon[2].y = (LONG)leftBottomY + y;

	HGDIOBJ oldObj = SelectObject( hSprDC, m_hBitmap );

	// Render a bitmap (rotated)
	PlgBlt(hBBDC, m_polygon, hSprDC, 0, 0, GetWidth(), GetHeight(), m_hBMMask, 0, 0);

	SelectObject( hSprDC, oldObj );
}

// Not tested.
void BitmapRot::Render( HDC hBBDC, HDC hSprDC, INT x, INT y, INT w, INT h, INT srcX, INT srcY )
{
	TranslatePosition( x, y );

	// Do fun math stuff!!!
	INT left	= -m_xRot;
	INT top		= -m_yRot;
	INT right	=  left + w;
	INT bottom	=  top + h;

	FLOAT cosAngle		= cosf(m_rotRad);
	FLOAT sinAngle		= sinf(m_rotRad);
	FLOAT leftTopX		= (FLOAT)(m_xRot + left*cosAngle - top*sinAngle);
	FLOAT leftTopY		= (FLOAT)(m_yRot + left*sinAngle + top*cosAngle);
	FLOAT rightTopX		= (FLOAT)(m_xRot + right*cosAngle - top*sinAngle);
	FLOAT rightTopY		= (FLOAT)(m_yRot + right*sinAngle + top*cosAngle);
	FLOAT leftBottomX	= (FLOAT)(m_xRot + left*cosAngle - bottom*sinAngle);
	FLOAT leftBottomY	= (FLOAT)(m_yRot + left*sinAngle + bottom*cosAngle);

	m_polygon[0].x = (LONG)leftTopX + x;
	m_polygon[0].y = (LONG)leftTopY + y;
	m_polygon[1].x = (LONG)rightTopX + x;
	m_polygon[1].y = (LONG)rightTopY + y;
	m_polygon[2].x = (LONG)leftBottomX + x;
	m_polygon[2].y = (LONG)leftBottomY + y;

	HGDIOBJ oldObj = SelectObject( hSprDC, m_hBitmap );

	// Render a bitmap (rotated)
	PlgBlt( hBBDC, m_polygon, hSprDC, srcX, srcY, m_bmInfo.bmWidth, m_bmInfo.bmHeight, m_hBMMask, srcX, srcY );

	SelectObject( hSprDC, oldObj );
}